package com.example.springbootdemo.dao;

import com.example.springbootdemo.database.QueryHelper;
import com.example.springbootdemo.entity.User;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;


//@Repository
public class UserDaoImpl implements UserDao {

    @Override
    public User registerUser(User user) {
        String sql = String.format("INSERT INTO users_table (customer_name,customer_nickname,customer_phone,customer_identification,customer_address,customer_password,customer_country) " +
                " VALUES ('%s', '%s','%s','%s','%s','%s','%s');", user.getCustomerName(), user.getCustomerNickname(), user.getCustomerPhone(), user.getCustomerIdentification(), user.getCustomerAddress(), user.getCustomerPassword(), user.getCustomerCountry());

        int res = QueryHelper.execuUpdate(sql);
        User user1 = null;
        if (res > 0) {
            try {
                String sql2 = "SELECT * FROM users_table WHERE customer_id = LAST_INSERT_ID();";
                ResultSet resultSet = QueryHelper.execuQuery(sql2);
                if (resultSet.next()) {
                    String customerName = resultSet.getString("customer_name");
                    String customerNickname = resultSet.getString("customer_nickname");
                    String customerPhone = resultSet.getString("customer_phone");
                    String customerIdentification = resultSet.getString("customer_identification");
                    String customerAddress = resultSet.getString("customer_address");
                    String customerPassword = resultSet.getString("customer_password");
                    String customerCountry = resultSet.getString("customer_country");
                    int customerId = resultSet.getInt("customer_id");
                    user1 = new User(customerId, customerName, customerNickname, customerPhone, customerIdentification, customerAddress, customerPassword, customerCountry);
                    QueryHelper.closeResource();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        return user1;
    }

    @Override
    public User searchUserByName(String username) {
        String sql = String.format("SELECT * FROM users_table WHERE customer_name = '%s'", username);
        ResultSet resultSet = QueryHelper.execuQuery(sql);
        User user1 = null;
        try {
            if (resultSet.next()) {
                String customerName = resultSet.getString("customer_name");
                String customerNickname = resultSet.getString("customer_nickname");
                String customerPhone = resultSet.getString("customer_phone");
                String customerIdentification = resultSet.getString("customer_identification");
                String customerAddress = resultSet.getString("customer_address");
                String customerPassword = resultSet.getString("customer_password");
                String customerCountry = resultSet.getString("customer_country");
                int customerId = resultSet.getInt("customer_id");
                user1 = new User(customerId, customerName, customerNickname, customerPhone, customerIdentification, customerAddress, customerPassword, customerCountry);
                QueryHelper.closeResource();

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user1;
    }

    @Override
    public Boolean deleteUserById(Integer id) {
        int res = QueryHelper.execuUpdate("DELETE FROM users_table WHERE customer_id = " + id);
        QueryHelper.closeResource();
        return res > 0;
    }
}
